'''
An RPG Modification for Source games using Source Python.

    Copyright (C) 2012 Steven Hartin

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

Created on 14 Dec 2012

@author: Steven Hartin
@file: ../sourcerpg/database/sqlite/parser.py
'''

from sqlite3 import dbapi2 as sqlite

from sourcerpg.database.parser.parser import BaseParser
from sourcerpg.database.parser import parser_commands, row_types
from sourcerpg.logging import logger, loglevels


data_types = {
    row_types.UNKNOWN: "NONE",
    row_types.BOOLEAN: "INTEGER",
    row_types.CHAR: "TEXT",
    row_types.INTEGER: "INTEGER",
    row_types.STRING: "TEXT",
    row_types.BLOB: "NONE",
    row_types.REAL: "REAL",
    row_types.DATE: "NUMERIC",
    row_types.DATETIME: "NUMERIC"
}


class Parser_API(BaseParser):
    """SQLite parser to parse the command queue for an SQLite interpretation"""
    def __init__(self, src_file, meta_file, database):
        """Constructor, create the database structure and parse the input

        @param str src_file A path to the XML source file
        @param str meta_file The file path of the meta data
        @param BaseDatabase database The database to parse the data to

        """
        super(Parser_API, self).__init__(src_file, meta_file, database)

    def parse_command_queue(self, command_queue, database):
        """Parse the command queue using SQLite as the API

        @param DatabseCommandQueue command_queue The queue of commands
        @param Database database The database to execute the commands on

        """
        logger.log("Parsing command queue")
        for command in command_queue:
            statement = ""
            if command.get_command() == parser_commands.CREATE_TABLE:
                logger.log("Received 'CREATE TABLE' command",
                    level=loglevels.INFO_LEVEL_2)
                statement = "CREATE TABLE IF NOT EXISTS %s(" % command.name
                for row in command.rows:
                    statement += "%s %s" % (row.name,
                        data_types.get(row.type_, "NONE"))
                    if row.primary_key is True:
                        statement += " PRIMARY KEY"
                    if row.auto_increment is True:
                        statement += " AUTOINCREMENT"
                    if row.not_null is True:
                        statement += " NOT NULL"
                    if row.unique is True:
                        statement += " UNIQUE"
                    if row.default is not None:
                        statement += " DEFAULT %s" % row.default
                    statement += ","
                statement = statement.rstrip(",") + ")"
            database.execute_raw_command(statement)
        database.connection.commit()
